package com.dinggou.controller;

import com.dinggou.common.ResponseCode;
import com.dinggou.common.ServerResponse;
import com.dinggou.entity.Course;
import com.dinggou.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {

    @Resource(name="courseServiceImpl")  //这个注解表示自动注入adminSeerviceImpl业务实现类
    @Autowired      //该注解表示后紧跟的变量进行封装，即自动产生getter和setter
    private CourseService courseService;

    @RequestMapping("/delete")
    @ResponseBody
    public ServerResponse deleteByPrimaryKey(Integer id){
        if(courseService.deleteByPrimaryKey(id)>0){
            return ServerResponse.createBySuccess(1,"删除管部门单位成功");
        }else{
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"删除数据失败");
        }
    }

    @RequestMapping("/insert")
    @ResponseBody
    public ServerResponse insert(Course record){

        Course course=new Course();

        course.setCode(record.getCode());
        course.setName(record.getName());
        course.setPeriod(record.getPeriod());
        course.setNature(record.getNature());

        if(courseService.insert(course)>0){
            return ServerResponse.createBySuccess("添加数据成功",course);
        }else{
            return ServerResponse.createByError();
        }
    }
    @RequestMapping("/insertSelective")
    @ResponseBody
    public ServerResponse insertSelective(Course record){

        Course course=new Course();

        course.setCode(record.getCode());
        course.setName(record.getName());
        course.setPeriod(record.getPeriod());
        course.setNature(record.getNature());

        if(courseService.insert(course)>0){
            return ServerResponse.createBySuccess("添加数据成功",course);
        }else{
            return ServerResponse.createByError();
        }
    }

    @RequestMapping("/selectByPrimaryKey")
    @ResponseBody
    public ServerResponse selectByPrimaryKey(Integer id, HttpServletRequest request, HttpServletResponse response) throws IOException {

        Course course = courseService.selectByPrimaryKey(id);

        if(course != null){
            return ServerResponse.createBySuccess(1,course);
        }else{
            return ServerResponse.createByErrorMessage("找不到管理员");
        }
    }


    @RequestMapping("/listall")
    @ResponseBody
    public ServerResponse listall(HttpServletRequest request, HttpServletResponse response){
        List<Course> courseList=courseService.selectAll();
        if(courseList.size()>0){
            return ServerResponse.createBySuccess(courseList.size(),courseList);
        }else{
            return ServerResponse.createByErrorMessage("找不到管理员信息");
        }
    }


    @RequestMapping("/updateByPrimaryKeySelective")
    @ResponseBody
    public ServerResponse updateByPrimaryKeySelective(Course record) {
        Course course=new Course();

        course.setId(record.getId());
        course.setCode(record.getCode());
        course.setName(record.getName());
        course.setPeriod(record.getPeriod());
        course.setNature(record.getNature());

        if(courseService.updateByPrimaryKeySelective(course)>0){
            return ServerResponse.createBySuccess("更新管理数据成功",course);
        }else{
            return ServerResponse.createByErrorMessage("更新管理数据失败");
        }

    }

    @RequestMapping("/updateByPrimaryKey")
    @ResponseBody
    public ServerResponse updateByPrimaryKey(Course record){

        Course course=new Course();

        course.setId(record.getId());
        course.setCode(record.getCode());
        course.setName(record.getName());
        course.setPeriod(record.getPeriod());
        course.setNature(record.getNature());

        if(courseService.updateByPrimaryKeySelective(course)>0){
            return ServerResponse.createBySuccess("更新管理数据成功",course);
        }else{
            return ServerResponse.createByErrorMessage("更新管理数据失败");
        }

    }
}